package com.test.controller;

import com.test.po.SnapShot;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RequestMapping("mg")
@RestController
public class MongoController {
    @Resource
    private MongoTemplate mo;

    @RequestMapping("test")
    public Object test() {
        List<String> strings = Arrays.asList("16623", "63910");

        //分组获得codes中时间最大的每条数据
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.group("code").max("marketDataTime").as("marketDataTime")
                , Aggregation.match(new Criteria("_id").in(strings))
        );
        String snapshot20200821 = "snapshot20200821";
        AggregationResults<HashMap> codeAndMarketDataTimeMapList = mo.aggregate(aggregation, snapshot20200821, HashMap.class);

        List<HashMap> results = codeAndMarketDataTimeMapList.getMappedResults();

        List<Criteria> criteria = new ArrayList<>();
        for (HashMap result : results) {
            String code = (String)result.get("_id");
            String time = (String)result.get("marketDataTime");
            criteria.add(Criteria.where("code").is(code).and("marketDataTime").is(time));
        }
        Criteria criteria1 = new Criteria();
        Query query = new Query();
        query.addCriteria(criteria1.orOperator(criteria.toArray(new Criteria[criteria.size()])));
        List<SnapShot> snapShots = mo.find(query, SnapShot.class, snapshot20200821);
        return snapShots;
    }
    @RequestMapping("test1")
    public Object test1() {
        List<String> strings = Arrays.asList("751078", "63910");

        //分组获得codes中时间最大的每条数据
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.group("code","ts").max("time").as("time"),
                Aggregation.match(Criteria.where("_id.code").in(strings))
        );
        String snapshot20200821 = "snapshot20200825";
        AggregationResults<HashMap> codeAndMarketDataTimeMapList = mo.aggregate(aggregation, snapshot20200821, HashMap.class);

        List<HashMap> results = codeAndMarketDataTimeMapList.getMappedResults();

        List<Criteria> criteria = new ArrayList<>();
        for (HashMap result : results) {
            String code = (String)result.get("_id");
            String time = (String)result.get("marketDataTime");
            criteria.add(Criteria.where("code").is(code).and("marketDataTime").is(time));
        }
        Criteria criteria1 = new Criteria();
        Query query = new Query();
        query.addCriteria(criteria1.orOperator(criteria.toArray(new Criteria[criteria.size()])));
        List<SnapShot> snapShots = mo.find(query, SnapShot.class, snapshot20200821);
        return snapShots;
    }
}
